﻿using System.Windows.Controls;
using System.Windows.Interactivity;

namespace MainCableTest.Utils
{
    public class AttachedProperty : Behavior<RichTextBox>
    {
        protected override void OnAttached()
        {
            base.OnAttached();
            AssociatedObject.TextChanged += AssociatedObject_TextChanged;
        }

        protected override void OnDetaching()
        {
            AssociatedObject.TextChanged -= AssociatedObject_TextChanged;
            base.OnDetaching();
        }

        private void AssociatedObject_TextChanged(object sender, TextChangedEventArgs e)
        {
            AssociatedObject.Dispatcher.BeginInvoke(new System.Action(() =>
            {
                AssociatedObject.ScrollToEnd();
            }));
        }
    }
}
